package com.tuojiahr.action;

import java.io.IOException;

import com.opensymphony.xwork2.Preparable;
import com.tuojiahr.common.BaseActionCommon;
import com.tuojiahr.domain.TuoJiaHrUser;
import com.tuojiahr.global.Constants;
import com.tuojiahr.service.UserService;
import com.tuojiahr.tools.StringUtil;

/**
 * 说明：
 *
 * @author wang.haibo
 * @date 2012-11-11 下午4:13:30
 */

public class UserAction extends BaseActionCommon implements Preparable {

	private static final long serialVersionUID = 1L;
	
	private UserService userService;
	private String message = "出错啦！！！";
	private String url = "adminmgr/user_openModifyPasswordView";
	
	private TuoJiaHrUser user = null;
	
	@Override
	public void prepare() throws Exception {
		user = getCurrUser();
	}
	
	/**
	 * 打开修改密码页面
	 * @return
	 */
	public String openModifyPasswordView() {
		if(null != user) {
			return "modify_pass_view";
		}
		return "msg";
	}

	/**
	 * 检验老密码是否正确
	 * @throws IOException
	 */
	public void checkOldPass() throws IOException {
		String result = "fault";
		if(null != user) {
			String oldPassword = request.getParameter("oldPassword");
			if(StringUtil.isNotBlank(oldPassword) && oldPassword.equals(user.getPassword())) {
				result = "success";
			}
		}
		response.getWriter().print(result);
		response.getWriter().close();
	}
	
	/**
	 * 修改密码
	 * @throws IOException 
	 */
	public String modifyUserPass() throws IOException {
		String newPassword = request.getParameter("newPassword");
		String oldPassword = request.getParameter("oldPassword");
		if(StringUtil.isNotBlank(newPassword) && StringUtil.isNotBlank(oldPassword) && oldPassword.equals(user.getPassword())) {
			user.setPassword(newPassword);
			try {
				userService.modifyPassword(user);
				message = "修改成功";
			} catch (Exception e) {
				Constants.ExceptionLog.error("##UserAction----modifyUserPass方法更新出错:", e);
			}
		}
		return "msg";
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
